﻿module Sustainalytics.Navigation {

    export class NavigationMenuController {

        // params
        public path: string;
        public siteMapCode: string;
        public startNodeCode: string;
        public startNodeLevel: number;
        public displayLevels: number;

        // sitemap used as a source of data
        private siteMap: SiteMap;

        // nodes from 1st level
        public nodes: SiteMapNode[];
        // nodes in chain (from the highest level which meets the actual path to the root)
        public nodeChain: SiteMapNode[];
        // selected nodes in sitemap
        public selectedNodes: SiteMapNode[];
        // current selected node (node in chain with the highest level)
        public currentNode: SiteMapNode;


        constructor(
            private $scope: INavigationMenuScope,
            private $rootScope: ng.IScope,
            private $timeout: ng.ITimeoutService,
            private $location: ng.ILocationService,
            private $route: ng.route.IRouteService,
            private $routeParams: ng.route.IRouteParamsService,
            private NavigationService: INavigationService) {

            var ctx = this;

            ctx.init();
        }

        private init() {
            var ctx = this;

            ctx.siteMapCode = ctx.$scope.siteMapCode ? ctx.$scope.siteMapCode : "SITE";
            ctx.startNodeCode = ctx.$scope.startNodeCode;
            ctx.startNodeLevel = parseInt(ctx.$scope.startNodeLevel ? ctx.$scope.startNodeLevel.toString() : '0');
            ctx.displayLevels = parseInt(ctx.$scope.displayLevels ? ctx.$scope.displayLevels.toString() : null);

            // get sitemap
            ctx.siteMap = ctx.NavigationService.getSiteMap(ctx.siteMapCode);

            // listen for sitemap changes
            ctx.$rootScope.$on('navigationSitemapChanged', function () {
                ctx.initNodes();
            });

            ctx.initNodes();
        }

        private initNodes() {
            var ctx = this;

            // get current path
            ctx.path = ctx.siteMap.path;

            var levelNode: SiteMapNode;

            // get node chain by path
            var nodeChain = ctx.siteMap.pathNodeChain;

            // get current node
            if (nodeChain && nodeChain.length > 0) {
                ctx.currentNode = nodeChain[nodeChain.length - 1];
            }

            if (ctx.startNodeCode) {
                // get node by code
                levelNode = ctx.siteMap.getNodeByCode(ctx.startNodeCode);
            }
            else {
                // get node by level
                if (nodeChain && nodeChain.length - 1 >= ctx.startNodeLevel) {
                    levelNode = nodeChain[ctx.startNodeLevel];
                }
            }

            // get current node nodes
            if (levelNode) {
                ctx.nodes = levelNode.nodes;
            }


            ctx.nodeChain = nodeChain.slice(ctx.startNodeLevel, ctx.displayLevels ? ctx.displayLevels : undefined);
        }
    }

    export var navigationMenuControllerName = 'NavigationMenuController';

    angular.module(navigationModuleName).controller(navigationMenuControllerName, NavigationMenuController);
} 